/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package concurrencia;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

/**
 *
 * @author eltorre
 */
public class Decorator extends Thread {
    
    private Decorator() 
    {
        
    };

    private static Decorator instance = null;
    
    public static Decorator getInstance()
    {
        if (instance == null)
            instance = new Decorator();
        return instance;
    }
    
    @Override
    public void run() 
    {
        while (true)
        {
            currIcon = 0;
            for (int i = 0; i < 100; i++)
            {
                currIcon = (currIcon + 1)%numIcons;
                currentProgress  = (currentProgress+1)%100;
                decorateLabels();
                try {
                    sleep(100);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Decorator.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            currIcon = -1;
            currState = (currState + 1) % numStates;
            decorateLabels();
            try {
                sleep(10000);
            } catch (InterruptedException ex) {
                Logger.getLogger(Decorator.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void decorateLabels()
    {
        if (currIcon == -1)
        {
            for (int i = 0; i < iconLabel.size(); i++)
                iconLabel.get(i).setIcon(idleIcon);
        }
        else
        {
             for (int i = 0; i < iconLabel.size(); i++)
                iconLabel.get(i).setIcon(busyIcons[currIcon]);
        }
        
         for (int i = 0; i < textLabel.size(); i++)
             textLabel.get(i).setText(states[currState]);
         for (int i = 0; i < progressBars.size(); i++)
             progressBars.get(i).setValue(currentProgress);
    }
    
    public Icon[] getBusyIcons() {
        return busyIcons;
    }

    public void setBusyIcons(Icon[] busyIcons) {
        this.busyIcons = busyIcons;
        this.numIcons = busyIcons.length;
    }

    public JLabel getIconLabel(int i) {
        return iconLabel.get(i);
    }

    public void addIconLabel(JLabel iconLabel) {
        this.iconLabel.add(iconLabel);
    }

    public Icon getIdleIcon() {
        return idleIcon;
    }

    public void setIdleIcon(Icon idleIcon) {
        this.idleIcon = idleIcon;
    }

    public JLabel getTextLabel(int i) {
        return textLabel.get(i);
    }

    public void addTextLabel(JLabel textLabel) {
        this.textLabel.add(textLabel);
    }
    
    public JProgressBar getProgressBar(int i)
    {
        return progressBars.get(i);
    }
    
    public void addProgressBar(JProgressBar j)
    {
        this.progressBars.add(j);
    }
    
    public void setCurrentProgress(int value)
    {
        currentProgress = value;
    }
    
    

    private final String states[] =
            {
                "Conectando con el servidor...",
                "Identificando...",
                "Bienvenido al sistema"
            };
    private final int numStates = 3;
    private int currState = 0;
    
    private Icon[] busyIcons;
    private Icon idleIcon;
    private int numIcons;
    private int currIcon = -1;
    
    private int currentProgress = 0;
    
    private ArrayList<JLabel> textLabel = new ArrayList<JLabel>();
    private ArrayList<JLabel> iconLabel = new ArrayList<JLabel>();
    private ArrayList<JProgressBar> progressBars = new ArrayList<JProgressBar>();
   
    

    
}
